package com.acme.bankapp.domain.bank.accounts;

import com.acme.bankapp.exceptions.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {

	private double balance;

	public AbstractAccount(final double amount) {
		this.balance = amount;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public double getBalance() {
		return balance;
	}

	public abstract void deposit(final double amount);

	public void withdraw(final double amount) throws NotEnoughFundsException {
		if (amount > balance) {
			throw new NotEnoughFundsException(amount - balance);
		}
		balance -= amount;
	}

	@Override
	public double decimalValue() {
		return Math.round(balance);
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}

		AbstractAccount that = (AbstractAccount) o;

		if (Double.compare(that.balance, balance) != 0) {
			return false;
		}

		return true;
	}


	@Override
	public int hashCode() {
		long temp = Double.doubleToLongBits(balance);
		return (int) (temp ^ (temp >>> 32));
	}

	@Override
	public String toString() {
		return "Account: balance=" + balance;
	}
}
